﻿using System;
using System.Windows.Forms;
using ICSharpCode.SharpDevelop.Gui;
using ICSharpCode.SharpDevelop.Project;

using GameControls;
using GameDesignControls;
using G.Model;

namespace GameSharpAddin
{
    public class GameTreePad : AbstractPadContent
    {
        static GameTreePad instance;
        bool disposed;
        GameTreeControl gameTreeControl;
        
        public GameTreePad()
        {
            instance = this;
            gameTreeControl = new GameTreeControl();
            
            ProjectService.SolutionClosed += SolutionClosed;
            ProjectService.SolutionLoaded += SolutionLoaded;
            
            WorkbenchSingleton.Workbench.ActiveViewContentChanged += delegate(object sender, EventArgs e)
            {
                BindToActiveGame();
            };
            WorkbenchSingleton.Workbench.ActiveContentChanged += delegate {
                BindToActiveGame();
            };
            
            BindToActiveGame();
        }

        void BindToActiveGame()
        {
            object obj = WorkbenchSingleton.Workbench.ActiveViewContent;
            if (obj is GameView) {
                GameView view = (GameView)obj;
                GameDisplayer displayer = view.GameTreePanel.BoardControl.GameDisplayer;
                gameTreeControl.GameDisplayer = displayer;
            } else {
                gameTreeControl.GameDisplayer = null;
            }
        }
        
        public static GameTreePad Instance {
            get {
                if (instance == null) {
                    ICSharpCode.SharpDevelop.PadDescriptor pad =
                        WorkbenchSingleton.Workbench.GetPad(typeof(GameTreePad));
                    if (pad != null) {
                        pad.CreatePad();
                    } else {
                        // Pad is not used (stripped-down SD version, e.g. SharpReport)
                        // Create dummy pad to prevent NullReferenceExceptions
                        instance = new GameTreePad();
                    }
                }
                return instance;
            }
        }
        
        public override object Control {
            get {
                return gameTreeControl;
            }
        }
        
        /// <summary>
        /// Cleans up all used resources
        /// </summary>
        public override void Dispose()
        {
            if (!disposed) {
                disposed = true;
                ProjectService.SolutionClosed -= SolutionClosed;
                ProjectService.SolutionLoaded -= SolutionLoaded;
                gameTreeControl.Dispose();
            }
        }
        
        void SolutionLoaded(object sender, EventArgs e)
        {
            BindToActiveGame();
        }
        
        void SolutionClosed(object sender, EventArgs e)
        {
            BindToActiveGame();
        }
    }
}
